﻿package rd.hardcore.selector 
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * ...
	 * @author Thibault Perret
	 */
	public class ScrollList extends Sprite
	{
		private var list:SelectList;
		private var maskList:MaskList;
		private var scrollBar:ScrollBar;
		
		public function ScrollList() 
		{
			list = new SelectList();
			list.y = 30;
			list.x = 5;
			addChild(list);
			list.alpha = 0;
			
			maskList = new MaskList();
			maskList.x = 1;
			maskList.y = 26;
			addChild(maskList);
			maskList.height = 0;
			list.mask = maskList;
			
			scrollBar = new ScrollBar_FC();
			scrollBar.x = 182;
			scrollBar.y = 29;
			addChild(scrollBar);
			scrollBar.hide();
			
			addEventListener(ScrollEvent.BAR_SCROLLING, barScrollingHandler);
			//addEventListener(ScrollEvent.LIST_SCROLLING, listScrollingHandler);
			list.addEventListener(MouseEvent.MOUSE_WHEEL, listScrollingHandler);
			
		}
		
		/**
		 * Scroll à partir de la liste
		 * @param	e
		 */
		private function listScrollingHandler(e:MouseEvent):void 
		{
			list.y += 2 * e.delta;
			if (list.y < (30 - list.height + maskList.height))	list.y = 30 - list.height + maskList.height;
			if (list.y > 30)	list.y = 30;
			var ratio:Number = (list.y -30) / -(list.height - maskList.height);
			scrollBar.setCursor(ratio);
		}
		
		/**
		 * scroll à partir de la barre prévue à cet effet
		 * @param	e
		 */
		private function barScrollingHandler(e:ScrollEvent):void 
		{
			list.y = 30 - e.ratio * (list.height - maskList.height);
		}
		
		public function setMask(size:int):void {
			if (size != 0) {
				size = size - 45 ;
				scrollBar.show();
			}else {
				scrollBar.hide();
			}
			list.alpha = 1;
			Tweener.addTween(scrollBar.bg, { height:size, time:1 } );
			Tweener.addTween(maskList, {height:size, time:1 } );
		}
		
	}

}